module nazo.singleton;
/**
 * デザインパターン、Singletonに簡単に対応します。
 * History:
 *          0.0.0.1 作成
 * Authors: Nazo
 * Version: 0.0.0.1
 * License: Public Domain
 */
///初めてgetInstanceが呼ばれたときにインスタンスを作成
class Singleton(T){
  private static T instance;
  private this(){};
  synchronized public static T getInstance(){
    if(instance is null)
      instance = new T();
    return instance;
  }
}

///mainの前にインスタンスを作成
class Singleton2(T){
  private static T instance;
  private this(){};
  public static T getInstance(){
    return instance;
  }
  synchronized static this(){
      instance = new T();
  }
}

class Test:Singleton!(Test){}
class Test2:Singleton2!(Test){}

unittest{
  assert(Test.getInstance()==Test.getInstance());
  assert(Test2.getInstance()==Test2.getInstance());
}
